package com.ecoolex.discount.sales.core.manager;

import com.alibaba.fastjson.JSON;
import com.ecoolex.discount.sales.core.dao.QuanMaoCardOrderDAO;
import com.ecoolex.discount.sales.core.external.pojo.QuanMaoOrderInfo;
import com.ecoolex.discount.sales.entity.QuanMaoCardOrder;
import com.ecoolex.discount.sales.entity.QuanMaoCardOrder.Column;
import com.ecoolex.discount.sales.entity.QuanMaoCardOrderExample;
import com.ecoolex.discount.sales.pojo.QueryQuanMaoCardOrderDTO;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.pojo.Sortings;
import com.ecoolex.framework.common.util.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.Instant;
import java.util.Date;

import static com.ecoolex.discount.sales.entity.QuanMaoCardOrder.NOT_DELETED;

@Service
public class QuanMaoCardOrderManager {
    @Autowired
    private QuanMaoCardOrderDAO dao;

    public void save(QuanMaoOrderInfo orderInfo, String createUser) {
        QuanMaoCardOrder record = new QuanMaoCardOrder();
        record.setOrderId(orderInfo.getOrderId());
        record.setOrderNumber(orderInfo.getOrderNumber());
        record.setMerchantsOrderNumber(orderInfo.getMerchantsOrderNumber());
        record.setCardName(orderInfo.getCardName());
        record.setCardType(orderInfo.getCardType());
        record.setCardNums(orderInfo.getCardNums());
        record.setStatus(orderInfo.getStatus());
        record.setPayTime(orderInfo.getPayTime());
        record.setFinallyTime(orderInfo.getFinallyTime());
        record.setChargeAccount(orderInfo.getChargeAccount());
        record.setOrderCardPassList(JSON.toJSONString(orderInfo.getOrderCardPassList()));
        record.setDeleted(NOT_DELETED);
        record.setCreateUser(createUser);
        record.setCreateTime(Date.from(Instant.now()));
        record.setUpdateUser(createUser);
        record.setUpdateTime(Date.from(Instant.now()));
        dao.insert(record);
    }

    public Pagination<QuanMaoCardOrder> query(QueryQuanMaoCardOrderDTO query, Sorting[] sorts, Page page) {
        QuanMaoCardOrderExample example = new QuanMaoCardOrderExample();
        QuanMaoCardOrderExample.Criteria criteria = example.createCriteria();

        if (Check.notNull(query.getOrderId())) {
            criteria.andOrderIdEqualTo(query.getOrderId());
        }
        if (Check.notNullOrTrimEmpty(query.getOrderNumber())) {
            criteria.andOrderNumberEqualTo(query.getOrderNumber());
        }
        if (Check.notNullOrTrimEmpty(query.getMerchantsOrderNumber())) {
            criteria.andMerchantsOrderNumberEqualTo(query.getMerchantsOrderNumber());
        }
        if (Check.notNull(query.getCardNo())) {
            criteria.andCardNoEqualTo(query.getCardNo());
        }
        if (Check.notNull(query.getCardType())) {
            criteria.andCardTypeEqualTo(query.getCardType());
        }
        if (Check.notNull(query.getStatus())) {
            criteria.andStatusEqualTo(query.getStatus());
        }
        if (Check.notNullOrTrimEmpty(query.getChargeAccount())) {
            criteria.andChargeAccountEqualTo(query.getChargeAccount());
        }

        criteria.andDeletedEqualTo(NOT_DELETED);

        example.orderBy(Sortings.builder(sorts).verify(Column::valueOf).toSql());
        example.setOffset(page.getFirstIndex());
        example.setRows(page.getPageSize());

        return Pagination.create(page, dao.countByExample(example), () -> dao.selectByExample(example));
    }
}